<?php

namespace App\Http\Controllers\Admin;

use App\DTOS\Admin\FunctionOpenDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\Functionopen;
use App\Models\Game\OnLine;
use App\Models\Game\Server;
use App\Services\Game\FunctionOpenService;
use App\Services\RedisService;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;

class FunctionopenController extends Controller
{
    private RedisService $RedisService;
    private FunctionOpenService $FunctionOpenService;

    /**
     * ServerController constructor.
     * @param RedisService $RedisService
     */
    public function __construct(RedisService $RedisService,FunctionOpenService $FunctionOpenService)
    {
        $this->RedisService = $RedisService;
        $this->FunctionOpenService = $FunctionOpenService;

    }

    public function paginate()
    {
        $params = request()->all();
        $serverIds = data_get($params,'serverIds');


        $data = [];
        MyLog::info('服务器列表: ' . json_encode($serverIds));
        if($serverIds){
            $serverIds=$serverIds;

        }else{
            $serverIds = Server::query()
                ->where('mysql_game_data_can_use', 1)
                ->orderBy('server_id', 'asc') // 按 server_id 升序排列
                ->pluck('server_id')
                ->toArray();
        }



        foreach ($serverIds as $serverId) {
            $functionopen = new Functionopen();
            $functionopen->setConnection('function_' . $serverId);

            $result = $functionopen->paginate(300)->toArray();
            MyLog::info('当前组装服务器ID: ' . json_encode($result['data']));
            if (!empty($result['data'])) { // 检查是否有数据
                foreach ($result['data'] as $item) { // 遍历数据数组
                    $entry = [
                        "serverId" => $serverId,
                        "id" => $item['id'],
                        "name" => $item['name'], // 使用数组索引
                        "close_type" => $item['close_type'],
                        "open_level" => $item['open_level'],
                        "open_type" => $item['open_type'],
                        "open_value1" => $item['open_value1'],
                        "open_value2" => $item['open_value2'],
                        "IsCloseChiji" => $item['is_close_chiji'],
                        "isCloseArena" => $item['is_close_arena'],
                        "isCloseCrossserver" => $item['is_close_crossserver']

                    ];
                    $data[] = $entry; // 将结果添加到数据数组
                }
            } else {
                MyLog::info('未找到在线数据，服务器ID: ' . json_encode($serverId));
            }
        }
        $res = [];
        $res['code'] = 0;
        $res['data'] =  $data;
        return ResponseUtil::json($res);
    }




    public function add(){
        $DTO = new FunctionOpenDTO(request()->all());
        $DTO->setState('1');
        $b = $this->FunctionOpenService->pushToServer($DTO);
        // 根据返回值更新状态
        if ($b) {
            return ResponseUtil::success();
        } else {
            return ResponseUtil::error();
        }
    }

    public function detail($id) {
        $functionopen = new Functionopen();
        $ids = request()->only(['ids'])['ids']; // 获取请求中的单个 ids

        $functionopen->setConnection('function_' . $id);

        // 使用 where() 查询单条数据
        $data = $functionopen->where('id', $ids)->first();

        // 检查是否找到数据
        if (!$data) {
            return ResponseUtil::error('数据未找到', 404);
        }

        return ResponseUtil::success($data);
    }

    public function editServer($id){
        $data = request()->only(['status']);
        $data['updated_at'] = date("Y-m-d H:i:s");
        $idArray=[];
        if (strpos($id, ',') !== false) {
            $idArray = explode(',', $id);
        }else{
            $idArray[] = $id;
        }
        MyLog::info('查看传入的ID'.json_encode($idArray));
        $b = Functionopen::query()->whereIn('server_id',$idArray)->update($data);
        if ($b){
            return ResponseUtil::success();
        } else {
            return ResponseUtil::error();
        }
    }

    public function edit($id){
        $DTO = new FunctionOpenDTO(request()->all());

        $DTO->setState('1');
        MyLog::info('查看传入的ID'.json_encode(request()->all()));

        $this->FunctionOpenService->pushToServer($DTO);
        return ResponseUtil::success();

    }

    public function delete($id){
        Functionopen::query()->where('server_id',$id)->delete();
        return ResponseUtil::success();
    }


    private function showCharTime($seconds){
        $minute = floor($seconds / 60);
        $second = $seconds % 60;
        return "{$minute}分{$second}秒";
    }


}
